/*!
 * 文件名称： main.c
 * 描    述： I2C-EEPROM读写实验主函数文件
 * 版本：     2023-12-03, V1.0
*/

/*
* GD32F303东方红开发板V1.0
* 淘宝商城：   https://juwo.taobao.com
* 技术交流：   https://www.gd32bbs.com/ask/
* 视频学习：   https://space.bilibili.com/475462605
* 微信公众号： gd32bbs
* Copyright    苏州聚沃电子科技有限公司
* 版权所有，盗版必究。
*/

#include "gd32f30x.h"
#include <stdio.h>
#include "bsp_led.h"
#include "driver_public.h"
#include "bsp_eeprom.h"
#include "driver_i2c.h"
#include "bsp_uart.h"

#define BUFFER_SIZE              256

/*!
* 说明     main函数
* 输入     无
* 输出     无
* 返回值   无
*/

int main(void)
{
	  uint16_t i;
    uint8_t i2c_buffer_write[BUFFER_SIZE];
    uint8_t i2c_buffer_read[BUFFER_SIZE];
	
	  driver_init();
	  /* 板载UART初始化 */
    bsp_uart_init(&BOARD_UART);
	  printf_log("Example of EEPROM write and read demo.\r\n");
    bsp_eeprom_init_AT24C16();
    /* 初始化写入buf */
    for(i = 0;i < BUFFER_SIZE;i++){ 
        i2c_buffer_write[i]=i;
        printf_log("0x%02X ",i2c_buffer_write[i]);
        if(15 == i%16){
            printf_log("\r\n");
        }
    }
		
		if(eeprom_buffer_write_AT24C16(i2c_buffer_write,0x0000,BUFFER_SIZE)==EEPROM_SUCCESS)
		{
			printf_log("EEPROM write successful.\r\n");
		}else{
			printf_log("EEPROM write failed.\r\n");
		}
		if(eeprom_buffer_read_AT24C16(i2c_buffer_read,0x0000,BUFFER_SIZE)==EEPROM_SUCCESS)
		{
			printf_log("EEPROM read successful.\r\n");
		}else{
			printf_log("EEPROM read failed.\r\n");
		}
		/* 比较写入和读取的数据是否一致 */
    for(i = 0;i < BUFFER_SIZE;i++){
        if(i2c_buffer_read[i] != i2c_buffer_write[i]){
            printf_log("0x%02X ", i2c_buffer_read[i]);
            printf_log("Err:data read and write aren't matching.\n\r");
					  while(1);
        }
        printf_log("0x%02X ", i2c_buffer_read[i]);
        if(15 == i%16){
            printf_log("\r\n");
        }
    }
		printf_log("data read and write are consistent!\n\r");
	  while (1)
	  {
	  }
}

